/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System.Web.UI;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Security;
using DotNetNuke.UI.WebControls;

using System.Collections;

using KDLocalization = DotNetNuke.ModuleToolkit.Localization.Localization;

#endregion

namespace DotNetNuke.ModuleToolkit.Controls
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// An Edit Control to Edit folders
    /// </summary>
    /// <history>
    /// 	[cnurse]	07/28/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class FolderEditControl : IntegerEditControl
    {

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a boolean indicating if the control is on a Host tab
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected bool IsHost
        {
            get
            {
                bool _IsHost = Null.NullBoolean;
                if (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId)
                    _IsHost = true;
                return _IsHost;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets the Portal Id to use when accessing the database to get a list of files and folders
        /// </summary>
        /// <remarks>
        /// Defaults to the value of the <see cref="global::DotNetNuke.Entities.Portals.PortalSettings.PortalId"/> property
        /// </remarks>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public int PortalId
        {
            get
            {
                int _PortalId = Null.NullInteger;
                if (!IsHost)
                    _PortalId = PortalSettings.PortalId;
                return _PortalId;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a <see cref="PortalSettings"/> object representing the current Portal Settings
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public PortalSettings PortalSettings
        {
            get
            {
                return PortalController.GetCurrentPortalSettings();
            }
        }

        #endregion

        #region Protected Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Renders the control in edit mode
        /// </summary>
        /// <param name="writer">An HtmlTextWriter to render the control to</param>
        /// <history>
        /// 	[cnurse]	04/20/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void RenderEditMode(HtmlTextWriter writer)
        {
            //Get the Folders
            string folderName;
            string viewRoles = Null.NullString;

            //Render the Select Tag
            ControlStyle.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
            writer.RenderBeginTag(HtmlTextWriterTag.Select);

            ArrayList folders = FileSystemUtils.GetFolders(PortalId);
            foreach (FolderInfo folder in folders)
            {
                if (folder.FolderPath == Null.NullString)
                {
                    folderName = KDLocalization.LocalizeString(this, "Root");
                    viewRoles = FileSystemUtils.GetRoles("", PortalId, "READ");
                }
                else
                {
                    folderName = folder.FolderPath;
                    viewRoles = FileSystemUtils.GetRoles(folderName, PortalId, "READ");
                }

                if (PortalSecurity.IsInRoles(viewRoles) && (folder.StorageLocation == (int)FolderController.StorageLocationTypes.InsecureFileSystem))
                {
                    //Add the Value Attribute
                    writer.AddAttribute(HtmlTextWriterAttribute.Value, folder.FolderID.ToString());

                    if (folder.FolderID == IntegerValue)
                        //Add the Selected Attribute
                        writer.AddAttribute(HtmlTextWriterAttribute.Selected, "selected");

                    //Render Option Tag
                    writer.RenderBeginTag(HtmlTextWriterTag.Option);
                    writer.Write(folderName);
                    writer.RenderEndTag();
                }
            }

            //Close Select Tag
            writer.RenderEndTag();
        } 

        #endregion

    }

}
